package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.order.IOrderLogService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.order.OrderLogParam;
import com.mdd.admin.vo.order.OrderLogListVo;
import com.mdd.admin.vo.order.OrderLogDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.OrderLog;
import com.mdd.common.mapper.order.OrderLogMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 订单日志实现类
 * @author panweiliang
 */
@Service
public class OrderLogServiceImpl implements IOrderLogService {
        
    @Resource
    OrderLogMapper orderLogMapper;

    /**
     * 订单日志列表
     *
     * @author panweiliang
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<OrderLogListVo>
     */
    @Override
    public PageResult<OrderLogListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<OrderLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderLogMapper.setSearch(queryWrapper, params, new String[]{
            "=:type:int",
            "=:channel:int",
            "=:orderId@order_id:int",
            "=:operatorId@operator_id:int",
            "=:content:str",
        });

        IPage<OrderLog> iPage = orderLogMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OrderLogListVo> list = new LinkedList<>();
        for(OrderLog item : iPage.getRecords()) {
            OrderLogListVo vo = new OrderLogListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtil.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 订单日志详情
     *
     * @author panweiliang
     * @param id 主键参数
     * @return OrderLog
     */
    @Override
    public OrderLogDetailVo detail(Integer id) {
        OrderLog model = orderLogMapper.selectOne(
                new QueryWrapper<OrderLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        OrderLogDetailVo vo = new OrderLogDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 订单日志新增
     *
     * @author panweiliang
     * @param orderLogParam 参数
     */
    @Override
    public void add(OrderLogParam orderLogParam) {
        OrderLog model = new OrderLog();
        model.setType(orderLogParam.getType());
        model.setChannel(orderLogParam.getChannel());
        model.setOrderId(orderLogParam.getOrderId());
        model.setOperatorId(orderLogParam.getOperatorId());
        model.setContent(orderLogParam.getContent());
        model.setCreateTime(System.currentTimeMillis() / 1000);
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        orderLogMapper.insert(model);
    }

    /**
     * 订单日志编辑
     *
     * @author panweiliang
     * @param orderLogParam 参数
     */
    @Override
    public void edit(OrderLogParam orderLogParam) {
        OrderLog model = orderLogMapper.selectOne(
                new QueryWrapper<OrderLog>()
                    .eq("id",  orderLogParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(orderLogParam.getId());
        model.setType(orderLogParam.getType());
        model.setChannel(orderLogParam.getChannel());
        model.setOrderId(orderLogParam.getOrderId());
        model.setOperatorId(orderLogParam.getOperatorId());
        model.setContent(orderLogParam.getContent());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        orderLogMapper.updateById(model);
    }

    /**
     * 订单日志删除
     *
     * @author panweiliang
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        OrderLog model = orderLogMapper.selectOne(
                new QueryWrapper<OrderLog>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        orderLogMapper.delete(new QueryWrapper<OrderLog>().eq("id", id));
    }

}
